package com.meringer.beans;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.meringer.beans.ejb.CarModelEJB;
import com.meringer.entities.Car;
import com.meringer.entities.CarModel;

@ManagedBean(name = "carModelBean")
@RequestScoped
public class CarModelBean {
	private List<CarModel> cars;  
	private CarModel selectedCar;  
	private List<CarModel> filteredCars;  
	@EJB
	CarModelEJB carModelEJB;
	
	public CarModel getSelectedCar() {  
        return selectedCar;  
    }  
  
    public void setSelectedCar(CarModel selectedCar) {  
        this.selectedCar = selectedCar;  
    }  
    public List<CarModel> getCars() {  
    	cars = carModelEJB.getCarModels();
        return cars;  
    }  
    public List<CarModel> getFilteredCars() {  
        return filteredCars;  
    }  
  
    public void setFilteredCars(List<CarModel> filteredCars) {  
        this.filteredCars = filteredCars;  
    }  
    public void saveBoughtCar(Integer modelID){
    	int result = carModelEJB.saveBoughtCar(modelID);
    	System.out.println(result);
    	if(result == 2){
    		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "You already have that car", null));
		} else if(result == 1){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "You do not have enough money to buy this car", null));			
		} else if(result == 0){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "You have successfully bought this car", null));
		}
    }
}
